package com.dq.domain.orders;

import com.alibaba.fastjson.JSONObject;
import com.dq.vo.baseVo.VO;

import com.dq.domain.message.oneself.centre.ApprovalUserCentre;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/9/17
 * @描述
 */
@Data
@Entity
@ApiModel("采购单")
public class OrgOrder extends Orders implements VO.ToVO {

    @ApiModelProperty("采购人（员工）")
    @ManyToOne
    protected SysUser staffUser;

//    @ApiModelProperty("金额，  放到 orders 中")
//    protected BigDecimal price;
    @ApiModelProperty("审批时间")
    protected Long passTime;

    @ApiModelProperty("采购状态")
    @Enumerated(EnumType.ORDINAL)
    public OrgOrderStatus orgOrderStatus;

    @ApiModelProperty("商品件数")
    protected Long payGoodsNum;
    @ApiModelProperty("分类数")
    protected Long cateNum;

    public enum OrgOrderStatus{
        submitPass("提交审批"),
        bossPass("boss审批确认"),
        financialPass("财务审批"),
        warehousePass("仓库确认"),
        orgPass("门店确认到货"),
        bossFefuse("boss拒绝"),
        financialRefuse("财务拒绝"),
        warehouseRefuse("仓库拒绝"),
        orgRefuse("门店到货拒绝"),
        ;

        public String name;

        OrgOrderStatus(String name) {
            this.name = name;
        }
    }
    public OrgOrder(){

    }
    public OrgOrder (SysUser staffUser, Org sysDept, BigDecimal price, OrgOrderStatus orgOrderStatus){
        this.staffUser = staffUser;
        this.org = sysDept;
        this.price = price;
        this.orgOrderStatus = orgOrderStatus;
    }

    @Data
    @ApiModel("采购订单视图")
    public static class Vo implements VO {

    }

    @Data
    @ApiModel("采购订单列表视图")
    public static class OrgOrderListVo{
        @ApiModelProperty("采购单id")
        protected Long id;
        @ApiModelProperty("采购单号")
        protected String orderNo;
        @ApiModelProperty("采购日期")
        protected String createTime;

        @ApiModelProperty("采购单状态")
        protected String statusEn;
        @ApiModelProperty("采购单状态")
        protected String statusCn;
        @ApiModelProperty("采购单总金额")
        protected BigDecimal totalAmount;
        @ApiModelProperty("审批时间")
        protected String passTime;
        @ApiModelProperty("商品件数")
        protected Long payGoodsNum;
        @ApiModelProperty("分类数")
        protected Long cateNum;
        @ApiModelProperty("备注")
        protected String remark;

        @ApiModelProperty("门店")
        protected Long orgId;
        @ApiModelProperty("门店")
        protected String orgName;
        @ApiModelProperty("采购人")
        protected Long staffUserId;
        @ApiModelProperty("采购人")
        protected String staffUserName;

        @ApiModelProperty("是否显示审批按钮")
        public Boolean isShow;
        @ApiModelProperty("用于判断下一步审核")
        public String nextCheckEn;
        @ApiModelProperty("流程图")
        public List<JSONObject> flowList;

        @ApiModelProperty("订单详情")
        protected List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos;
    }

    @Data
    @ApiModel("门店采购单详情")
    public static class OrgOrderDetailVo{
        @ApiModelProperty("采购单号")
        protected String orderNo;
        @ApiModelProperty("订单所属门店")
        protected String orgName;
        @ApiModelProperty("采购日期")
        protected String createTime;
        @ApiModelProperty("采购单状态")
        protected String status;
        @ApiModelProperty("采购单总金额")
        protected BigDecimal totalAmount;
        @ApiModelProperty("审批时间")
        protected String passTime;
        @ApiModelProperty("商品列表")
        protected List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos;

    }

    public OrgOrderDetailVo toOrgOrderDetailVo(List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos){
        OrgOrder.OrgOrderDetailVo orgOrderDetailVo = new OrgOrder.OrgOrderDetailVo();
        orgOrderDetailVo.orderNo = fmt(this.getOrdersNo());
        orgOrderDetailVo.createTime = fmt(this.getCreateTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgOrderDetailVo.status = fmt(this.getOrgOrderStatus().name);
        orgOrderDetailVo.totalAmount = fmt(this.getPrice());
        orgOrderDetailVo.passTime = fmt(this.getPassTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgOrderDetailVo.orgOrderInfoVos =orgOrderInfoVos;
        orgOrderDetailVo.orgName = fmt(this.getOrg().getSimpleName());
        return orgOrderDetailVo;

    }

    public OrgOrderListVo toOrgOrderListVo(){
        OrgOrder.OrgOrderListVo orgOrderListVo = new OrgOrder.OrgOrderListVo();
        orgOrderListVo.id = this.getId();
        orgOrderListVo.orderNo = fmt(this.getOrdersNo());
        orgOrderListVo.statusEn = this.getOrgOrderStatus() == null ? "" : this.getOrgOrderStatus().toString();
        orgOrderListVo.statusCn = this.getOrgOrderStatus() == null ? "" : this.getOrgOrderStatus().name;
        orgOrderListVo.payGoodsNum = fmt(this.getPayGoodsNum());
        orgOrderListVo.cateNum = fmt(this.getCateNum());
        orgOrderListVo.totalAmount = fmt(this.getPrice());
        orgOrderListVo.createTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgOrderListVo.passTime = fmt(this.getPassTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgOrderListVo.remark = fmt(this.getRemark());

        orgOrderListVo.orgId = this.getOrg().getId();
        orgOrderListVo.orgName = this.getOrg().getSimpleName();
        orgOrderListVo.staffUserId = this.getStaffUser().getId();
        orgOrderListVo.staffUserName = this.getStaffUser().getUserRealcName();

        // 是否显示【审批/到货确认】按钮
        Map<String, Object> mapVo = ApprovalUserCentre.getIsShow(this);
        orgOrderListVo.isShow = mapVo.get("isShow") == null ? null : (boolean) mapVo.get("isShow");
        orgOrderListVo.nextCheckEn = mapVo.get("nextCheckEn") == null ? null : mapVo.get("isShow").toString();
        // 流程图
        orgOrderListVo.flowList = ApprovalUserCentre.getFlowList(this);

        return orgOrderListVo;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();

        return vo;
    }

    @Data
    @NoArgsConstructor
    public static class OrgOrderAdd{
        private Long productId;
        private Long num;
    }
}
